package com.ruoyi.table.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.table.domain.DiningTable;
import com.ruoyi.table.domain.dto.DiningTableSaveBatchDTO;
import com.ruoyi.table.service.DiningTableService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-24 01:14:45
 */
@RestController
@RequestMapping("/dining_table")
@RequiredArgsConstructor
public class DiningTableController extends BaseController {

    private final DiningTableService diningTableService;

    /**
     * 列表
     *
     * @param diningTable 餐桌
     * @param pageQuery   页面查询
     * @return {@link TableDataInfo}<{@link DiningTable}>
     */
    @GetMapping("/page")
    @SaCheckPermission("table:diningTable:select")
    public TableDataInfo<DiningTable> pageDiningTable(DiningTable diningTable, PageQuery pageQuery) {
        return diningTableService.pageDiningTable(diningTable, pageQuery);
    }

    /**
     * 更新状态
     *
     * @param diningTable 餐桌
     * @return {@link R}<{@link Void}>
     */
    @PutMapping("/status")
    @SaCheckPermission("table:diningTable:update")
    @RepeatSubmit
    public R<Void> updateStatus(@RequestBody DiningTable diningTable) {
        return toAjax(diningTableService.updateById(diningTable));
    }

    /**
     * 通过id获取餐桌
     *
     * @param id id
     * @return {@link R}<{@link DiningTable}>
     */
    @GetMapping("/{id}")
    @SaCheckPermission("table:diningTable:select")
    public R<DiningTable> getDiningTableById(@PathVariable("id") Long id) {
        return R.ok(diningTableService.getById(id));
    }

    /**
     * 保存餐桌
     *
     * @param diningTable 餐桌
     * @return {@link R}<{@link Void}>
     */
    @RepeatSubmit
    @PostMapping
    @SaCheckPermission("table:diningTable:save")
    public R<Void> saveDiningTable(@RequestBody DiningTable diningTable) {
        return toAjax(diningTableService.saveDiningTable(diningTable));
    }

    /**
     * 更新餐桌
     *
     * @param diningTable 餐桌
     * @return {@link R}
     */
    @PutMapping
    @RepeatSubmit
    @SaCheckPermission("table:diningTable:update")
    public R<Void> updateDiningTable(@RequestBody DiningTable diningTable) {
        return toAjax(diningTableService.updateDiningTable(diningTable));
    }


    /**
     * 删除
     *
     * @param ids id
     * @return {@link R}<{@link Void}>
     */
    @DeleteMapping
    @SaCheckPermission("table:diningTable:delete")
    public R<Void> delete(@RequestBody List<Long> ids) {
        return toAjax(diningTableService.removeByIds(ids));
    }

    /**
     * 批量保存餐桌
     */
    @PostMapping("/batch")
    @RepeatSubmit
    @SaCheckPermission("table:diningTable:save")
    public R<Void> saveDiningTableBatch(@RequestBody DiningTableSaveBatchDTO dto) {
        boolean b = diningTableService.saveDiningTableBatch(dto);
        return toAjax(b);
    }

}
